<script setup lang="ts">
import type { ListParams } from '@/types'
import type { PropertyI } from '@/types/property.d.ts'
import { onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { EmployeeI } from '@/types/employee'
import {
  employeeDelApi,
  employeeGetListApi,
  employeeResetPwdApi,
} from '@/apis/employee'
import { formatTagType } from '@/utils/format'
import EmployeeDialog from './components/EmployeeDialog.vue'
import type { RoleI } from '@/types/system'
import { roleGetRoleListApi } from '@/apis/role'

const listParams = ref<ListParams>({
  page: 1,
  pageSize: 8,
  name: '',
})
const list = ref<EmployeeI[]>([])
const total = ref(0)
const getList = async () => {
  const res = await employeeGetListApi(listParams.value)
  list.value = res.rows
  total.value = res.total
}
getList()
const onSearch = () => {
  listParams.value.page = 1
  listParams.value.pageSize = 8
  getList()
}
const changePage = (page: number) => {
  listParams.value.page = page
  getList()
}
const changePageSize = (pageSize: number) => {
  listParams.value.page = 1
  listParams.value.pageSize = pageSize
  getList()
}
const onDel = async (id: number) => {
  await employeeDelApi(id)
  getList()
  ElMessage.success('删除成功~')
}
const resetPwd = async (id: number) => {
  await ElMessageBox.confirm('确定将密码重置为"123456"?', '', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  await employeeResetPwdApi(id)
  ElMessage.success('密码重置成功~')
}
/**
 * 弹层
 */
const dialogVisible = ref(false)
const dialogParams = ref<EmployeeI>({
  id: undefined,
  name: '',
  roleId: undefined,
  status: undefined,
  userName: '',
  phonenumber: '',
})
let roleList: RoleI[] = []
onMounted(async () => {
  roleList = await roleGetRoleListApi()
})
const onEdit = (row: EmployeeI) => {
  dialogParams.value = { ...row, createTime: undefined, roleName: undefined }
  dialogVisible.value = true
}
</script>

<template>
  <div class="page-container">
    <!-- 搜索区域 -->
    <el-form :inline="true" class="header-container">
      <el-form-item label="员工姓名:">
        <el-input placeholder="请输入员工姓名" v-model="listParams.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <el-button type="primary" @click="dialogVisible = true">添加员工</el-button>
    <!-- 表格区域 -->
    <el-table :data="list">
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column label="员工姓名" align="center" prop="name" />
      <el-table-column label="登录账号" align="center" prop="userName" />
      <el-table-column label="联系方式" align="center" prop="phonenumber" />
      <el-table-column label="角色" align="center" prop="roleName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="{ row }">
          <el-tag :type="formatTagType(row.status)">{{
            row.status === 0 ? '未启用' : '启用'
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="添加时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column label="操作" align="center" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="onEdit(row)">编辑</el-button>
          <el-popconfirm title="确定删除吗?" @confirm="onDel(row.id)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
          <el-button type="text" @click="resetPwd(row.id)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="listParams.page"
      v-model:page-size="listParams.pageSize"
      layout="total, prev, pager, next, slot, jumper"
      :total="total"
      @current-change="changePage"
    >
      <el-select
        style="width: 95px"
        v-model="listParams.pageSize"
        @change="changePageSize"
      >
        <el-option label="8条/页" :value="8" />
        <el-option label="10条/页" :value="10" />
        <el-option label="15条/页" :value="15" />
      </el-select>
    </el-pagination>
    <!-- 添加弹层区域 -->
    <employee-dialog
      v-model:dialogVisible="dialogVisible"
      v-model:dialogParams="dialogParams"
      :roleList="roleList"
      @success="getList"
    />
  </div>
</template>

<style lang="scss" scoped>
.page-container {
  padding: 20px;

  .el-form {
    margin-bottom: 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);

    ::v-deep(.el-form-item__label) {
      color: rgb(39, 39, 39);
      font-weight: 500;
    }
  }

  .el-table {
    width: 100%;
    height: 400px;
    margin: 20px 0;

    ::v-deep(.el-table__header th) {
      background-color: rgb(237, 237, 237, 0.9);
      color: rgb(43, 37, 37);
      padding: 10px 0;
    }

    ::v-deep(.el-table__body tr) {
      height: 50px;
    }
  }

  .el-pagination {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
